package com.fcscs.cservice.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fcscs.core.service.FcsManager;
import com.fcscs.core.util.CommonUtil;
import com.fcscs.cservice.dao.RestaurantDao;
import com.fcscs.cservice.domain.ActivityLogs;
import com.fcscs.cservice.domain.PropertyConfigRestaurant;
import com.fcscs.cservice.domain.Restaurant;
import com.fcscs.cservice.domain.RestaurantConfig;
import com.fcscs.cservice.domain.model.RestaurantConfigModel;
import com.fcscs.cservice.domain.model.RestaurantConfiguration;
import com.fcscs.cservice.domain.model.RestaurantView;
import com.fcscs.cservice.service.RestaurantManager;

@Service("restaurantManager")
public class RestaurantManagerImpl extends FcsManager implements RestaurantManager {
	
	@Autowired
	private RestaurantDao dao;
//	private static Logger LOGGER = Logger.getLogger(RestaurantManagerImpl.class);
	
	@Override
	public <E extends Restaurant> E get(Class<E> cls, long id) {
		return dao.get(cls, id);
	}

	@Override
	public Restaurant get(long id) {
		return dao.get(id);
	}

	@Override
	public Boolean delete(Restaurant loc) {
		return dao.delete(loc);
	}

	@Override
	public void permDelete(Restaurant loc) {
		dao.permDelete(loc);
	}

	@Override
	public <E extends Restaurant> E save(E loc) {
		if(loc == null){
			return loc;
		}
		loc.setOrgId(CommonUtil.getUserContext().getOrgId());
		//TODO: propertyId
		Restaurant update = loc;
		if(!loc.isNew()){
			update = get(loc.getId());
			update.setDailyOperationEndtime(loc.getDailyOperationEndtime());
			update.setEcAckEmail(loc.getEcAckEmail());
			update.setEcAckTimeout(loc.getEcAckTimeout());
		}
		transferBaseVals(loc, update, loc.isNew());
//		//***************************
//		ErrorEnums err = new BookingServiceValidators.RestaurantValidator().validate(loc, loc.getId() == null);
//		if (err != null) {
//			LOGGER.debug(err);
//			return null;
//		}
//		loc.setOrgId(ManagerContextValueUtil.getOrgId());
//		loc.setCreatedBy(ManagerContextValueUtil.getUserId());
		return dao.save(loc);
	}

	public Restaurant  progressRestaurant(Restaurant loc){
		if(loc == null){
			return loc;
		}
		loc.setOrgId(CommonUtil.getUserContext().getOrgId());
		//TODO: propertyId
		Restaurant update = loc;
		if(!loc.isNew()){
			update = get(loc.getId());
			update.setDailyOperationEndtime(loc.getDailyOperationEndtime());
			update.setEcAckEmail(loc.getEcAckEmail());
			update.setEcAckTimeout(loc.getEcAckTimeout());
		}
		transferBaseVals(loc, update, loc.isNew());
		return update;
	}
	
	
	@Override
	public List<Restaurant> getRestaurants(long orgId, Long propId) {
		return dao.getRestaurants(orgId, propId);
	}

	@Override
	public List<RestaurantView> getRestaurantViews(long orgId,Long propId) {
		return dao.getRestaurantViews(orgId,propId);
	}

	@Override
	public RestaurantConfigModel saveRestaurantConfigModel(RestaurantConfigModel model) {
		if (model == null) {
			return model;
		}
		//********************************save propertyconfigRestaurant
		progressPropertyConRest(model.getPropertyConRestaurant());
		List<RestaurantConfiguration> restConfigList = model.getRestMsgList();
		for(RestaurantConfiguration rc : restConfigList){
			//**************************save restaurant
			progressRestaurant(rc.getRestaurant());
			List<RestaurantConfig> restaurantConfigList = rc.getRestConfigDetail();
			for(RestaurantConfig rcf : restaurantConfigList ){
				progressRestaurantConfig(rcf);
			}
		}
		return dao.saveRestaurantConfigModel(model);
	}

	@Override
	public RestaurantConfigModel getRestaurantConfigModel(long orgId,Long propId) {
		return dao.getRestaurantConfigModel(orgId, propId);
	}

	@Override
	public RestaurantConfig saveRestaurantConfig(RestaurantConfig restConfig) {
		RestaurantConfig update = progressRestaurantConfig(restConfig);
		return dao.saveRestaurantConfig(update);
	}

	private RestaurantConfig progressRestaurantConfig(RestaurantConfig restConfig){
		if(restConfig == null){
			return restConfig;
		}
		restConfig.setOrgId(CommonUtil.getUserContext().getOrgId());
		//TODO: propertyId
		RestaurantConfig update = restConfig;
		if(!restConfig.isNew()){
			update = getRestaurantConfig(restConfig.getId());
			update.setConfigTypeEnum(restConfig.getConfigTypeEnum());
			update.setNote1(restConfig.getNote1());
			update.setNote2(restConfig.getNote2());
			update.setNote3(restConfig.getNote3());
		}
		transferBaseVals(restConfig, update, restConfig.isNew());
		return update;
	}
	
	
	@Override
	public RestaurantConfig getRestaurantConfig(long restConfigId) {
		return dao.getRestaurantConfig(restConfigId);
	}
	
	@Override
	public List<RestaurantConfig> getRestaurantConfig(long orgId, Long propId,long restaId) {
		return dao.getRestaurantConfig(orgId, propId, restaId);
	}

	
	
	@Override
	public RestaurantConfiguration saveRestaurantConfig(RestaurantConfiguration resc) {
		return dao.saveRestaurantConfig(resc);
	}

	@Override
	public PropertyConfigRestaurant savePropertyConfigRestaurant(PropertyConfigRestaurant propConfigRest) {
		PropertyConfigRestaurant update =progressPropertyConRest(propConfigRest);
		return dao.savePropertyConfigRestaurant(update);
	}

	private PropertyConfigRestaurant progressPropertyConRest(PropertyConfigRestaurant propConfigRest){
		if (propConfigRest == null) {
			return propConfigRest;
		}
		propConfigRest.setOrgId(CommonUtil.getUserContext().getOrgId());
		//TODO:
//		propConfigRest.setPropertyId(CommonUtil.getUserContext().getPropertyId());
		PropertyConfigRestaurant update = propConfigRest;
		if (!propConfigRest.isNew()) {
			update = dao.getPropertyConfigRestaurant(propConfigRest.getId());
			update.setReservationReqAckPopup(propConfigRest.getReservationReqAckPopup());
			update.setSecLangReservationReqAckPopup(propConfigRest.getSecLangReservationReqAckPopup());
			update.setBookingConfirmTitle(propConfigRest.getBookingConfirmTitle());
			update.setSecLangBookingConfirmTitle(propConfigRest.getSecLangBookingConfirmTitle());
			update.setBookingConfirmBody(propConfigRest.getBookingConfirmBody());
			update.setSecLangBookingConfirmBody(propConfigRest.getBookingConfirmBody());
			update.setRestaurantBkTempTitle(propConfigRest.getSecLangRestaurantBkTempTitle());
			update.setSecLangRestaurantBkTempTitle(propConfigRest.getSecLangRestaurantBkTempTitle());
			update.setRestaurantBkTempBody(propConfigRest.getRestaurantBkTempBody());
			update.setSecLangRestaurantBkTempBody(propConfigRest.getSecLangRestaurantBkTempBody());
			update.setBookingReqMsgTemplate(propConfigRest.getBookingReqMsgTemplate());
			update.setSecLangBookingReqMsgTemplate(propConfigRest.getSecLangBookingReqMsgTemplate());
		}
		transferBaseVals(propConfigRest, update, propConfigRest.isNew());
		return update;
	}
	
	@Override
	public PropertyConfigRestaurant getPropertyConfigRestaurant(long propConfigRestId) {
		return dao.getPropertyConfigRestaurant(propConfigRestId);
	}

	@Override
	public PropertyConfigRestaurant getPropertyConfigRestaurant(long orgId,Long propetyId) {
		return dao.getPropertyConfigRestaurant(orgId, propetyId);
	}
	
	@Override
	public List<ActivityLogs> simpFindActivityLogs(long orgId, Long propId,
			Long userId, Date startDate, Date endDate, int start, int resultCnt) {
		return dao.simpFindActivityLogs(orgId, propId, userId, startDate, endDate, start, resultCnt);
	}

	@Override
	public int simpFindActivityLogsCnt(long orgId, Long propId, Long userId,Date startDate, Date endDate) {
		return dao.simpFindActivityLogsCnt(orgId, propId, userId, startDate, endDate);
	}

	@Override
	public List<ActivityLogs> advaFindActivityLogs(long orgId, Long propId,String actionName, String entityName,
			Long userId, String detail,Date startDate, Date endDate, int start, int resultCnt) {
		return dao.advaFindActivityLogs(orgId, propId, actionName, entityName, userId, 
				detail, startDate, endDate, start, resultCnt);
	}

	@Override
	public int advaFindActivityLogsCnt(long orgId, Long propId,String actionName, String entityName, 
			Long userId, String detail,Date startDate, Date endDate) {
		return dao.advaFindActivityLogsCnt(orgId, propId, actionName, entityName, userId, detail, startDate, endDate);
	}

}
